package com.sunmouse.toolkit.fragment.tv;


import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.sunmouse.toollib.utils.ToastUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.adapter.TvListAdapter;
import com.sunmouse.toolkit.fragment.BaseFragment;
import com.sunmouse.toolkit.widget.RefreshScrollView;
import com.sunmouse.toolkit.widget.SwipeListView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListFragment extends BaseFragment {


    @BindView(R.id.m_lv)
    SwipeListView mLv;
    Unbinder unbinder;
    @BindView(R.id.m_scroll)
    RefreshScrollView mScroll;


    private SwipeMenuCreator mCreator;
    private ArrayList<String> items;
    private TvListAdapter mAdapter;

    public ListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_swipe_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {

        mCreator = new SwipeCreator();
        this.mLv.setMenuCreator(mCreator);
        this.mLv.setOnMenuItemClickListener(mItemClick);
        this.mLv.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);

        mScroll.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mScroll.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {

            }
        });

        items = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            items.add("item-" + (i + 1));
        }


        mAdapter = new TvListAdapter(items, getActivity());
        this.mLv.setAdapter(mAdapter);
        this.mLv.setOnFocusChangeListener(mOnFocusChangeListener);
        this.mLv.setItemsCanFocus(true);




    }







    /**
     * focus changed listener
     */
    private View.OnFocusChangeListener mOnFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            boolean focus = hasFocus;
            if (hasFocus) {
                ((ViewGroup) mLv.getChildAt(0)).getChildAt(0).requestFocus();
            }
            //mLv.setSelection(0);
        }
    };


    /**
     * swipe creator
     */
    class SwipeCreator implements SwipeMenuCreator {
        @Override
        public void create(SwipeMenu menu) {
            menu.addMenuItem(generateMenuItem("open", R.color.colorPrimary));
            menu.addMenuItem(generateMenuItem("delete", R.color.colorAccent));

        }
    }

    /**
     * generate menu item
     *
     * @return
     */
    private SwipeMenuItem generateMenuItem(String title, @ColorRes int colorId) {
        SwipeMenuItem item = new SwipeMenuItem(getActivity());
        item.setBackground(colorId);
        item.setWidth(180);
        item.setTitle(title);
        item.setTitleSize(13);
        item.setTitleColor(R.color.white);
        return item;
    }

    /**
     * set on menu item click
     */
    private SwipeMenuListView.OnMenuItemClickListener mItemClick = new SwipeMenuListView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
            switch (index) {
                case 0:
                    //open
                    ToastUtils.simpleToast(getActivity(), "open click");
                    break;
                case 1:
                    //delete
                    ToastUtils.simpleToast(getActivity(), "delete click");
                    break;
            }
            return false;
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
